﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

using System.Data.SqlClient;
using System.ServiceModel.Activation;
using System.ServiceModel;

namespace HomeHealthWebService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class HomeHealthService : IHomeHealthService
    {
        static SqlConnection myConnection = new SqlConnection("Data Source=tomicalab.com;Initial Catalog=HomeHealth;User ID=sa;Password=sa123abc!!!");

        public Patient InsertNewPatient(Patient createPatient)
        {
            return createPatient;
        }

        public List<Patient> GetAllPatient()
        {
            List<Patient> results = new List<Patient>();
            Patient result;
            try
            {
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand("SELECT * FROM Patient", myConnection);
                SqlDataReader myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    result = new Patient();
                    result.PatientID = Convert.ToString(myReader.GetValue(0));
                    result.Name = Convert.ToString(myReader.GetValue(1));
                    result.DateOfBirth = Convert.ToString(myReader.GetValue(2));
                    result.PlaceOfBirth = Convert.ToString(myReader.GetValue(3));
                    result.IDNo = Convert.ToString(myReader.GetValue(4));
                    result.MaritalStatus = Convert.ToString(myReader.GetValue(5));
                    result.Address = Convert.ToString(myReader.GetValue(6));
                    result.PhysicianID = Convert.ToString(myReader.GetValue(7));
                    result.NurseID = Convert.ToString(myReader.GetValue(8));
                    result.PhoneNumber = Convert.ToString(myReader.GetValue(9));
                    result.StatusType = Convert.ToString(myReader.GetValue(10));
                    result.CreateDate = Convert.ToString(myReader.GetValue(11));
                    result.UpdateDate = Convert.ToString(myReader.GetValue(12));

                    results.Add(result);
                }
                myConnection.Close();
            }
            catch (Exception ex)
            {
                return null;
            }
            return results;
        }

        public Patient GetAPatient(string id)
        {
            Patient result = new Patient();
            try
            {
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand("SELECT * FROM Patient WHERE PatientID = " + id, myConnection);
                SqlDataReader myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    result.PatientID = Convert.ToString(myReader.GetValue(0));
                    result.Name = Convert.ToString(myReader.GetValue(1));
                    result.DateOfBirth = Convert.ToString(myReader.GetValue(2));
                    result.PlaceOfBirth = Convert.ToString(myReader.GetValue(3));
                    result.IDNo = Convert.ToString(myReader.GetValue(4));
                    result.MaritalStatus = Convert.ToString(myReader.GetValue(5));
                    result.Address = Convert.ToString(myReader.GetValue(6));
                    result.PhysicianID = Convert.ToString(myReader.GetValue(7));
                    result.NurseID = Convert.ToString(myReader.GetValue(8));
                    result.PhoneNumber = Convert.ToString(myReader.GetValue(9));
                    result.StatusType = Convert.ToString(myReader.GetValue(10));
                    result.CreateDate = Convert.ToString(myReader.GetValue(11));
                    result.UpdateDate = Convert.ToString(myReader.GetValue(12));
                }
                myConnection.Close();
            }
            catch (Exception ex)
            {
                return null;
            }
            return result;
        }

        public Patient UpdatePatient(string id, Patient updatePatient)
        {
            return updatePatient;
        }

        public void DeletePatient(string id)
        {
            try
            {
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand("DELETE FROM Patient WHERE PatientID = " + id, myConnection);
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                return;
            }
            return;
        }

        public Nurse InsertNewNurse(Nurse createNurse)
        {
            return createNurse;
        }

        public List<Nurse> GetAllNurse()
        {
            List<Nurse> results = new List<Nurse>();
            Nurse result;
            try
            {
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand("SELECT * FROM Nurse", myConnection);
                SqlDataReader myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    result = new Nurse();
                    result.ID = Convert.ToString(myReader.GetValue(0));
                    result.Name = Convert.ToString(myReader.GetValue(1));
                    result.PhoneNumber = Convert.ToString(myReader.GetValue(2));
                    result.Address = Convert.ToString(myReader.GetValue(3));
                    result.Specialist = Convert.ToString(myReader.GetValue(4));
                    result.Hospital = Convert.ToString(myReader.GetValue(5));
                    result.HospitalHotline = Convert.ToString(myReader.GetValue(6));
                    result.CreateDate = Convert.ToString(myReader.GetValue(7));
                    result.UpdateDate = Convert.ToString(myReader.GetValue(8));
                    results.Add(result);
                }
                myConnection.Close();
            }
            catch (Exception ex)
            {
                return null;
            }
            return results;
        }

        public Nurse GetANurse(string id)
        {
            Nurse result = new Nurse();
            try
            {
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand("SELECT * FROM Patient WHERE PatientID = " + id, myConnection);
                SqlDataReader myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    result.ID = Convert.ToString(myReader.GetValue(0));
                    result.Name = Convert.ToString(myReader.GetValue(1));
                    result.PhoneNumber = Convert.ToString(myReader.GetValue(2));
                    result.Address = Convert.ToString(myReader.GetValue(3));
                    result.Specialist = Convert.ToString(myReader.GetValue(4));
                    result.Hospital = Convert.ToString(myReader.GetValue(5));
                    result.HospitalHotline = Convert.ToString(myReader.GetValue(6));
                    result.CreateDate = Convert.ToString(myReader.GetValue(7));
                    result.UpdateDate = Convert.ToString(myReader.GetValue(8));
                }
                myConnection.Close();
            }
            catch (Exception ex)
            {
                return null;
            }
            return result;
        }

        public Nurse UpdateNurse(string id, Nurse updateNurse)
        {
            return updateNurse;
        }

        public void DeleteNurse(string id)
        {
            try
            {
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand("DELETE FROM Nurse WHERE ID = " + id, myConnection);
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                return;
            }
            return;
        }

        public Physician InsertNewPhysician(Physician createPhysician)
        {
            return createPhysician;
        }

        public List<Physician> GetAllPhysician()
        {
            List<Physician> results = new List<Physician>();
            Physician result;
            try
            {
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand("SELECT * FROM Physician", myConnection);
                SqlDataReader myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    result = new Physician();
                    result.ID = Convert.ToString(myReader.GetValue(0));
                    result.Name = Convert.ToString(myReader.GetValue(1));
                    result.PhoneNumber = Convert.ToString(myReader.GetValue(2));
                    result.Address = Convert.ToString(myReader.GetValue(3));
                    result.Specialist = Convert.ToString(myReader.GetValue(4));
                    result.Hospital = Convert.ToString(myReader.GetValue(5));
                    result.HospitalHotline = Convert.ToString(myReader.GetValue(6));
                    result.CreateDate = Convert.ToString(myReader.GetValue(7));
                    result.UpdateDate = Convert.ToString(myReader.GetValue(8));
                    results.Add(result);
                }
                myConnection.Close();
            }
            catch (Exception ex)
            {
                return null;
            }
            return results;
        }

        public Physician GetAPhysician(string id)
        {
            Physician result = new Physician();
            try
            {
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand("SELECT * FROM Patient WHERE PatientID = " + id, myConnection);
                SqlDataReader myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    result.ID = Convert.ToString(myReader.GetValue(0));
                    result.Name = Convert.ToString(myReader.GetValue(1));
                    result.PhoneNumber = Convert.ToString(myReader.GetValue(2));
                    result.Address = Convert.ToString(myReader.GetValue(3));
                    result.Specialist = Convert.ToString(myReader.GetValue(4));
                    result.Hospital = Convert.ToString(myReader.GetValue(5));
                    result.HospitalHotline = Convert.ToString(myReader.GetValue(6));
                    result.CreateDate = Convert.ToString(myReader.GetValue(7));
                    result.UpdateDate = Convert.ToString(myReader.GetValue(8));
                }
                myConnection.Close();
            }
            catch (Exception ex)
            {
                return null;
            }
            return result;
        }

        public Physician UpdatePhysician(string id, Physician updatePhysician)
        {
            return updatePhysician;
        }

        public void DeletePhysician(string id)
        {
            try
            {
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand("DELETE FROM Physician WHERE ID = " + id, myConnection);
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                return;
            }
            return;
        }

        public List<SicknessHistory> GetSicknessHistory(string id)
        {
            List<SicknessHistory> results = new List<SicknessHistory>();
            SicknessHistory result;
            try
            {
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand("SELECT * FROM Physician", myConnection);
                SqlDataReader myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    result = new SicknessHistory();
                    result.ID = Convert.ToString(myReader.GetValue(0));
                    result.Disease = Convert.ToString(myReader.GetValue(1));
                    result.Time = Convert.ToString(myReader.GetValue(2));
                    result.Symtom = Convert.ToString(myReader.GetValue(3));
                    result.Body = Convert.ToString(myReader.GetValue(4));
                    result.Cause = Convert.ToString(myReader.GetValue(5));
                    result.NurseID = Convert.ToString(myReader.GetValue(6));
                    result.Patient = Convert.ToString(myReader.GetValue(7));
                    result.Medication = Convert.ToString(myReader.GetValue(8));
                    result.Remark = Convert.ToString(myReader.GetValue(9));
                    results.Add(result);
                }
                myConnection.Close();
            }
            catch (Exception ex)
            {
                return null;
            }
            return results;
        }

    }

    
}
